home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-12-17 | 8.9 KB | 400 lines | [TEXT/MPS ] |
- /* Rotate.c is the Macintosh shell for the Rotate.a algorithms */
-
- #include <Quickdraw.h>
- #include <Desk.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include <Fonts.h>
- #include <Memory.h>
- #include <Menus.h>
- #include <OSEvents.h>
- #include <Packages.h>
- #include <Resources.h>
- #include <SegLoad.h>
- #include <Toolutils.h>
- #include <Windows.h>
- #include "Rotate.h"
-
- typedef enum {Rot0, RotH, RotV, RotX,
- RotL, RotR, RotFL, RotFR} RotationSw;
-
- pascal void ROTATE(rotation, sourceMap, destMap)
- RotationSw rotation;
- BitMap *sourceMap, *destMap;
- extern;
-
- /* Global Data objects, used by routines external to main(). */
- Boolean doneFlag; /* Becomes TRUE when File/Quit chosen */
- WindowRecord wRecord;
- WindowPtr myWindow; /* Referenced often */
- EventRecord myEvent;
- WindowPtr whichWindow;
- long lastTick,tick;
- long stretch ; /* used to vary the bounds of the Pict */
-
- /*******************************/
- /* BitMap Image Declarations */
- /*******************************/
-
- #define xInset 10
- #define yInset 20
-
- #define MyLeft 0
- #define MyRight ((kWindowWidth - 3*xInset) / 2)
- #define MyTop 0
- #define MyBottom MyRight
-
- #define leftInset xInset
- #define rightInset (kWindowWidth - (MyRight - MyLeft) - xInset)
- BitMap sourceMap, destMap;
-
- /***************************/
- /* BitMap Image Routines */
- /***************************/
-
- InitImage(Image)
- BitMap *Image;
- {
- int myRowBytes = ((MyRight - MyLeft + 15) / 16) * 2;
- int mySize = myRowBytes * (MyBottom - MyTop);
-
- Image->baseAddr = NewPtr(mySize); /* allocate memory */
- Image->rowBytes = myRowBytes;
- SetRect(&Image->bounds, MyLeft, MyTop, MyRight, MyBottom);
- }
-
- BuildImage(Image)
- BitMap *Image;
- { /* Create an Image to rotate */
- GrafPort myPort,*savePort;
-
- GetPort(&savePort);
- OpenPort(&myPort);
- SetPortBits(Image);
- RectRgn(myPort.visRgn, &Image->bounds);
- PortSize(Image->bounds.right, Image->bounds.bottom);
- SetPort(savePort);
- ClosePort(&myPort);
- }
-
- ShowImage(Image,myXoff,myYoff)
- BitMap *Image;
- short myXoff,myYoff;
- {
- GrafPtr myPort;
-
- GetPort(&myPort);
- OffsetRect(&Image->bounds, myXoff, myYoff);
- CopyBits(Image, &myPort->portBits, &Image->bounds, &Image->bounds, srcXor, 0L);
- OffsetRect(&Image->bounds, -myXoff, -myYoff);
- }
-
- RotateImage(rotation, sourceMap, destMap)
- RotationSw rotation;
- BitMap *sourceMap, *destMap;
- {
- if (destMap->baseAddr) { /* If there an old image */
- ShowImage(destMap, rightInset, yInset); /* Display image to erase it */
- DisposPtr(destMap->baseAddr); /* then, free it */
- destMap->baseAddr = 0;
- }
- WaitForTick(); /* wait for the clock to tick */
- ROTATE(rotation, sourceMap, destMap); /* Do the rotation */
- ShowTick(rotation); /* Time it */
- ShowImage(destMap, rightInset, yInset); /* Display the rotated image */
- }
-
- GarbageImage(Image)
- BitMap *Image;
- {
- int size = Image->rowBytes * (Image->bounds.bottom - Image->bounds.top);
- char *p = Image->baseAddr;
- char *g = 100; /* just some low memory address */
-
- ShowImage(Image, leftInset, yInset); /* Display image to erase it */
- while (size--)
- *p++ = *g++;
- ShowImage(Image, leftInset, yInset); /* Display Image */
- }
-
- FillImage(Image, color)
- BitMap *Image;
- int color;
- {
- int size = Image->rowBytes * (Image->bounds.bottom - Image->bounds.top);
- char *p = Image->baseAddr;
-
- ShowImage(Image, leftInset, yInset); /* Display image to erase it */
- while (size--)
- *p++ = color;
- ShowImage(Image, leftInset, yInset); /* Display Image */
- }
-
- GetPict(Image)
- BitMap *Image;
- {
- int refNum, picWidth, picHeight;
- GrafPort myPort,*savePort;
- PicHandle srcPict;
- Rect *irp;
-
- if (Image->baseAddr) { /* If there an old image */
- ShowImage(Image, leftInset, yInset); /* Display image to erase it */
- DisposPtr(Image->baseAddr); /* then, free it */
- Image->baseAddr = 0;
- }
- /* Load the PICT, compute target Rect to center in image window, draw it */
- refNum = OpenResFile("\pBell");
- srcPict = (PicHandle)GetResource('PICT', rBell); /* load PICT resource */
- if (srcPict == 0L) {
- doAlert(rAlert,kNoBell);
- ExitToShell();
- }
- HLock((Handle)srcPict);
- irp = &((*srcPict)->picFrame); /* pointer to PICT's bounds */
- picWidth = scale*(irp->right - irp->left); /* compute PICT width */
- picHeight = scale*(irp->bottom - irp->top); /* compute PICT height */
-
- picWidth += stretch;
- picHeight += stretch;
-
- Image->bounds.left = 0; /* PICT size Rect */
- Image->bounds.top = 0;
- Image->bounds.right = Image->bounds.left + picWidth;
- Image->bounds.bottom = Image->bounds.top + picHeight;
- Image->rowBytes = ((picWidth + 15) / 16) * 2;
- Image->baseAddr = NewPtr(Image->rowBytes * Image->bounds.bottom);
-
- GetPort(&savePort);
- OpenPort(&myPort);
- SetPortBits(Image);
- RectRgn(myPort.visRgn, &Image->bounds);
- PortSize(Image->bounds.right, Image->bounds.bottom);
- DrawPicture(srcPict,&Image->bounds);
- FrameRect(&Image->bounds);
- SetPort(savePort);
- ClosePort(&myPort);
-
- HUnlock((Handle)srcPict); /* we no longer need resource */
- CloseResFile(refNum);
- ShowImage(Image, leftInset, yInset); /* Display sourceMap */
- }
-
- /******************************/
- /* Low level system routines */
- /******************************/
-
- doAlert(theAlert,theInx)
- short theAlert,theInx;
- {
- Str255 theText;
-
- GetIndString(theText,rTextStr,theInx);
- SysBeep(4);
- ParamText(theText,0,0,0);
- Alert(theAlert,0);
- }
-
- WaitForTick()
- {
- tick = TickCount();
- while (tick == TickCount())
- ; /* wait */
- lastTick = tick;
- }
-
- ShowNum(n,x,y)
- int n,x,y;
- {
- Rect r;
- Str255 myStr;
-
- NumToString(n, myStr);
- SetRect(&r, x, y-10, x+40, y+2);
- EraseRect(&r);
- MoveTo(x,y);
- DrawString(myStr);
- }
-
- ShowTick(rotation)
- RotationSw rotation;
- {
- int n,x,y;
-
- n = TickCount() - lastTick;
- x = 160 + 40 * rotation;
- y = 3*yInset + MyBottom;
- MoveTo(90,y);
- DrawString("\p Ticks = ");
- ShowNum(n, x, y);
- }
-
- ShowSize(sourceMap)
- BitMap *sourceMap;
- {
- int x,y;
-
- x = 160;
- y = 2*yInset + MyBottom;
- MoveTo(90,y);
- DrawString("\pBounds = ");
- ShowNum(sourceMap->bounds.left, x, y);
- x += 40;
- ShowNum(sourceMap->bounds.top, x, y);
- x += 40;
- ShowNum(sourceMap->bounds.right, x, y);
- x += 40;
- ShowNum(sourceMap->bounds.bottom, x, y);
- }
-
-
- /****************************/
- /* Event handling routines */
- /****************************/
-
- doEvent()
- {
- SystemTask();
-
- if (GetNextEvent(everyEvent, &myEvent))
- switch (myEvent.what) {
- case mouseDown:
- switch (FindWindow(myEvent.where, &whichWindow)) {
- case inSysWindow:
- SystemClick(&myEvent, whichWindow);
- break;
- case inMenuBar:
- doCommand(MenuSelect(myEvent.where));
- break;
- case inContent:
- if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- break;
- }
- break;
-
- case keyDown:
- case autoKey:
- if (myWindow == FrontWindow()) {
- if (myEvent.modifiers & cmdKey) {
- doCommand(MenuKey(myEvent.message & charCodeMask));
- }
- }
- break;
-
- case updateEvt:
- if ((WindowPtr) myEvent.message == myWindow) {
- BeginUpdate(myWindow);
- EraseRect(&myWindow->portRect);
- ShowImage(&sourceMap, leftInset, yInset);/* Display image */
- ShowImage(&destMap, rightInset, yInset); /* Display destMap */
- ShowSize(&sourceMap);
- EndUpdate(myWindow);
- }
- break;
- }
- }
-
- setupMenus()
- {
- Handle menuBar;
-
- menuBar = GetNewMBar(rMenuBar); /* read menus into menu bar */
- if (menuBar == nil) ExitToShell();
- SetMenuBar(menuBar); /* install menus */
- AddResMenu(GetMHandle(mApple), 'DRVR'); /* add DA names to Apple menu */
- DrawMenuBar();
- }
-
- NewScreen()
- {
- myWindow = GetNewWindow(rWindow, nil, (WindowPtr) -1);
- SetPort(myWindow);
- }
-
- /* Process mouse clicks in menu bar */
- doCommand(mResult)
- long mResult;
- {
- short theMenu, theItem;
- char daName[256];
- GrafPtr savePort;
- RotationSw rot;
-
- theMenu = HiWord(mResult);
- theItem = LoWord(mResult);
- switch (theMenu) {
- case mApple:
- if (theItem == aboutMeCommand)
- doAlert(rMsg,kAboutText);
- else {
- GetItem(GetMHandle(mApple), theItem, daName);
- GetPort(&savePort);
- (void) OpenDeskAcc(daName);
- SetPort(savePort);
- }
- break;
-
- case mFile:
- if (theItem == quitCommand)
- doneFlag = true; /* Request exit */
- break;
-
- case mImage:
- switch (theItem) {
- case GarbageCommand:
- GarbageImage(&sourceMap);
- break;
- case BlackCommand:
- FillImage(&sourceMap, 0xFF);
- break;
- case WhiteCommand:
- FillImage(&sourceMap, 0);
- break;
- case BellCommand:
- stretch = 0; /* reset stretch */
- GetPict(&sourceMap);
- break;
- case StretchCommand:
- stretch++;
- GetPict(&sourceMap);
- break;
- } /*endsw theItem*/
- ShowSize(&sourceMap); /* display the bounds */
- break;
-
- case mRotate:
- if (theItem == RotAllCommand) {
- for (rot=0; rot < 8; rot++)
- RotateImage(rot, &sourceMap, &destMap);
- } else
- RotateImage(theItem - Rot0Command, &sourceMap, &destMap);
- break;
- }
- HiliteMenu(0);
- }
-
- main()
- {
- MaxApplZone();
- MoreMasters();
-
- /* Initialization traps */
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- setupMenus();
- doneFlag = false;
- NewScreen();
- InitImage(&sourceMap);
- BuildImage(&sourceMap);
- while (!doneFlag)
- doEvent();
- }
-